/*
 * FolderProcessor.hpp
 *
 *  Created on: 25.03.2011
 *      Author: niea
 */

#pragma once

// for pthreads
#define _REENTRANT

#include <string>
#include <vector>

#include "FolderDescription.hpp"
#include "../../rapidxml-1.13/rapidxml.hpp"
#include "../../rapidxml-1.13/rapidxml_print.hpp"
#include "../common/ClientEntryList.hpp"
#include "../tcp/TCPListenerSubscriber.hpp"
#include "../tcp/TCPMessenger.h"
#include "../file-client/FileClientI.hpp"

#include "FolderManagerI.hpp"

namespace efs
{

class FolderManager : public FolderManagerI
{
public:
	FolderManager(std::string const& root_path, int folder_monitor_period, Settings const& settings);
	virtual ~FolderManager();

	void compareWithRemoteFolder(std::string remote_folder_xml_string);
	std::string getXMLString();

	bool start();
	void stop();

	void processTCPMessage(std::string const& message);


private:

	std::string getXMLString(rapidxml::xml_document<> const &xml);

	void perseXML(rapidxml::xml_document<> &xml, std::string const& xml_string);

	void removeFiles(std::vector<std::string> const& list);

	void removeFolders(std::vector<std::string> const& list);

	void makeNewFolders(std::vector<std::string> const& list);

	void getFiles(ClientEntry &remote_entry, std::string const& remote_root_path, std::vector<std::string> const &list);

	static void * threadProcStarter(void *base_class_pointer);
	void threadProc();

	rapidxml::xml_node<> *prepareXML(rapidxml::xml_document<> &xml) const;

	rapidxml::xml_document<> m_xml;

	FolderDescription m_folder_description;

	pthread_t m_thread;
	bool m_stop_req;
	bool m_stop_monitoring;
	bool m_activate_transmission;
	bool m_receive_enable;
};


}

